package com.example.musictest.ui.home;

import android.content.Context;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModel;

import com.fw.mediasession.media.bean.BaseBean;
import com.fw.mediasession.media.bean.Music;
import com.fw.mediasession.media.bean.MyPicture;
import com.fw.mediasession.media.bean.MyVideo;
import com.fw.mediasession.media.mediasession.MyMediaConnection;
import com.fw.mediasession.media.mediastore.inter.MediaDataInterface;
import com.orhanobut.logger.Logger;
import com.zy.untils.TimeUtils;

import java.util.ArrayList;

import javax.inject.Inject;

import dagger.hilt.android.lifecycle.HiltViewModel;
import dagger.hilt.android.qualifiers.ApplicationContext;

@HiltViewModel
public class HomeViewModel extends ViewModel {

    private String TAG="HomeViewModel: ";
    private MutableLiveData<String> mText;
    public MutableLiveData<ArrayList<Music>> musics = new MutableLiveData<>();
    public MutableLiveData<ArrayList<MyVideo>> videos = new MutableLiveData<>();
    public MutableLiveData<ArrayList<MyPicture>> pictures = new MutableLiveData<>();

   public MediaDataInterface mediaDataInterface;
   public MyMediaConnection connection;
    public MutableLiveData<String> title =new MutableLiveData<>();
    public MutableLiveData<Integer> seekPosition =new MutableLiveData<>();
    public MutableLiveData<Integer> maxPosition =new MutableLiveData<>();
    public  MutableLiveData<BaseBean> currentData=new MutableLiveData<>();
    public  MutableLiveData<String> startTime=new MutableLiveData<>();
    public  MutableLiveData<String> totalTime=new MutableLiveData<>();


    @Inject
    public HomeViewModel(MediaDataInterface ss, MyMediaConnection connection) {
        mText = new MutableLiveData<>();
        mText.setValue("This is home fragment");
        mediaDataInterface = ss;
        this.connection = connection;
        seekPosition=getSeekPosition();
        currentData=getCurrentData();
//        title=getTitle();
        currentData.observeForever(new Observer<BaseBean>() {
            @Override
            public void onChanged(BaseBean baseBean) {
                title.setValue("标题："+baseBean.title+",专辑:"+baseBean.album+",歌手："+baseBean.artist);
               maxPosition.setValue((int) baseBean.duration);
               Logger.d(TAG+"数据更新 +maxPosition"+ baseBean.duration+"标题："+baseBean.title+",专辑:"+baseBean.album+",歌手："+baseBean.artist);
                String s = TimeUtils.longToTimeStr(baseBean.duration);
                totalTime.setValue(s);

            }
        });
        seekPosition.observeForever(new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                String s = TimeUtils.longToTimeStr(integer);
                startTime.setValue(s);
            }
        });
    }

    public LiveData<String> getText() {
        return mText;
    }

    /**
     * 获取mediaStore数据
     */
    public void getData() {
        musics.setValue(mediaDataInterface.getMusicData());
        videos.setValue(mediaDataInterface.getVideoData());
        pictures.setValue(mediaDataInterface.getPictureData());
    }
    public void updateMediaStoreData(@ApplicationContext Context context){
//        MediaScannerConnection.scanFile(context,);
    }

    public void getMediaStoreMusics() {
        musics.setValue(mediaDataInterface.getMusicData());
    }

    public void getMediaStoreVideos() {
        videos.setValue(mediaDataInterface.getVideoData());
    }

    public void getMediaStorePictures() {
        pictures.setValue(mediaDataInterface.getPictureData());
    }



    public void playMediaList(ArrayList<? extends BaseBean> medias, int index, int type){
        connection.playList(medias,index,type);
    }
    public void play(){
        connection.play();
    }
    public void pause(){
        connection.pause();
    }
    public void skipToNext(){
        connection.skipToNext();
    }
    public void skipToPrevious(){
        connection.skipToPrevious();
    }

    public MutableLiveData<BaseBean> getCurrentData(){
      return   connection.getCurrentData();
    }
    public MutableLiveData<String> getTitle(){

        return  connection.getTitle();
    }
    public MutableLiveData<Integer> getSeekPosition(){

        return  connection.getPosition();
    }
    public void seekTo(Long l){
        connection.seekTo(l);
    }
    public void nextMode(){
        connection.nextMode();
    }
}